﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;

namespace Ghost.Tasks.Wiki
{
    public class EmbeddedResourceResolver : XmlUrlResolver
    {
        public string Pattern { get; private set;}
        public Assembly Assembly { get; private set;}
        public EmbeddedResourceResolver(Assembly assembly, string pattern)
        {
            Assembly = assembly;
            Pattern = pattern;
        }

        public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            string fileName = absoluteUri.Segments[absoluteUri.Segments.Length - 1];
            string resourcePath = String.Format(Pattern, Assembly.GetName().Name, fileName);
            Stream stream = Assembly.GetManifestResourceStream(resourcePath);
            if (stream != null)
                return stream;
            return base.GetEntity(absoluteUri, role, ofObjectToReturn);
        }
    }


}
